package ch05_1;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Helper {
    private final static String PROPERTIES_PATH = "helper.properties";

    private static Properties properties;

    private Helper() {
        readProperties();
    }

    /* 静态内部类获得外部类实例，在构造方法中读取配置文件 */
    private static class PropStaticInner {

        private static final Helper filename = new Helper();
    }

    public static Helper getInstance() {
        return PropStaticInner.filename;
    }

    private static void readProperties() {
        properties = new Properties();
        InputStream in = Helper.class.getClassLoader().getResourceAsStream(PROPERTIES_PATH);
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
    }

    public Properties getProps() {
        return properties;
    }
}
